<?php

namespace app\shop\controller\user;

use app\common\model\user\AttestationInfo;
use app\common\model\user\Sms as SmsModel;
use app\common\model\user\UserWithdrawal;
use app\shop\controller\Controller;
use app\shop\model\user\RegisterInfo;
use app\shop\model\user\User as UserModel;
use app\shop\model\user\Grade;
use think\facade\Db;

/**
 * 用户管理
 */
class User extends Controller
{
    /**
     * 商户列表
     */
    public function index($nickName = '', $gender = null, $reg_date = '', $grade_id = null,$classify='')
    {
        $list = UserModel::getList($nickName, $grade_id, $reg_date, $gender = -1,$classify, $this->postData());
        $GradeModel = new Grade();
        $grade = $GradeModel->getLists();
        return $this->renderSuccess('', compact('list', 'grade'));
    }


    /**
     * 删除用户
     */
    public function delete($user_id)
    {
        // 用户详情
        $model = UserModel::detail($user_id);
        if ($model && $model->setDelete()) {
            return $this->renderSuccess('删除成功');
        }
        return $this->renderError($model->getError() ?: '删除失败');
    }


    /**
     * 添加用户
     */
    public function add()
    {
        $model = new UserModel;
        // 新增记录
        if ($model->add($this->request->param())) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 用户充值
     */
    public function recharge($user_id, $source)
    {
        // 用户详情
        $model = UserModel::detail($user_id);

        if ($model->recharge($this->store['user']['user_name'], $source, $this->postData('params'))) {
            return $this->renderSuccess('操作成功');
        }
        return $this->renderError($model->getError() ?: '操作失败');
    }

    /**
     * 等级改用户
     */
    public function edit($user_id)
    {
        // 用户详情
        $model = UserModel::detail($user_id);
        // 修改记录
        if ($model->updateGrade($this->postData())) {
            return $this->renderSuccess('修改成功');
        }
        return $this->renderError($model->getError() ?: '修改失败');
    }

    public function register_info($user_id)
    {
        $where = array('user_id' => $user_id);
        $list = Db::name('register_info')->where($where)->order('id desc')->find();
        return $this->renderSuccess('', [
            'info' => $list,
        ]);
    }

    public function register_info_post()
    {
        $data = $this->postData();
        $model = new RegisterInfo();
        $res = $model->where(array('id'=>$data['info']['id']))->update(array('status'=>$data['status']));

        if($res){
            return $this->renderSuccess('修改成功');
        }else{
            return $this->renderError('修改失败');
        }
    }

    public function attestation_info()
    {
        $user_id = $this->request->param("user_id");
        $model = new AttestationInfo();
        $info = $model->where(array("user_id"=>$user_id))->find();
        return $this->renderSuccess('', [
            'info' => $info,
        ]);
    }
    public function attestation_info_post()
    {
        $data = $this->request->param();
        $model = new AttestationInfo();
        $id = $data['info']['id'];
        $status = $data['status'];
        $res = $model->where(array("id"=>$id))->update(array('status'=>$status));

        if($res){
            return $this->renderSuccess('修改成功');
        }else{
            return $this->renderError('修改失败');
        }
    }

    //获取提现记录
    public function withdrawl()
    {
        $data = $this->postData();
        $model = new UserWithdrawal();
        $list = $model->get_list($data['Params']);
        return $this->renderSuccess('', compact('list'));
    }

    public function withdrawl_detail()
    {
        $id = $this->request->param("id");
        $model = new UserWithdrawal();
        $info = $model->details($id);
        return $this->renderSuccess('', compact('info'));
    }
    public function withdrawl_post()
    {
        $data = $this->postData();

        $model = new UserWithdrawal();
        $res = $model->edit($data);
        if($res){
            return $this->renderSuccess('修改成功');
        }else{
            return $this->renderError('修改失败');
        }
    }

    public function withdrawl_del()
    {
        $id = $this->request->param('id');
        $model = new UserWithdrawal();
        $res = $model->where(array('id'=>$id))->update(array('is_delete'=>1));
        if($res){
            return $this->renderSuccess('删除成功');
        }else{
            return $this->renderError('删除失败');
        }
    }

}
